package com.netbetter.langzheng.commons.gears.utils;

import java.util.ArrayList;
import java.util.List;

import com.allen_sauer.gwt.log.client.Log;
import com.netbetter.langzheng.commons.third.utils.ApacheStringUtils;

public class StringUtils extends ApacheStringUtils {

  /**
   * 代替+
   * 
   * @param args
   * @return
   */
  public static String combinStr(Object... args) {
    StringBuilder sb = new StringBuilder();
    for (Object obj : args) {
      if (obj != null)
        sb.append(obj);
    }
    return sb.toString();
  }

  public static String upperCase(String str, int idx) {
    if (str == null) {
      return null;
    }
    if (idx < 0 || idx > str.length() - 1) {
      return str;
    }

    return str.substring(0, idx) + str.substring(idx, idx + 1).toUpperCase()
        + str.substring(idx + 1);
  }

  public static String lowerCase(String str, int idx) {
    if (str == null) {
      return null;
    }
    if (idx < 0 || idx > str.length() - 1) {
      return str;
    }

    return str.substring(0, idx) + str.substring(idx, idx + 1).toLowerCase()
        + str.substring(idx + 1);
  }

  public static Integer[] toIntArray(Object[] objs) {
    if (objs == null)
      return null;
    Integer[] ints = new Integer[objs.length];
    for (int i = 0; i < ints.length; i++) {
      if (objs[i] != null) {
        try {
          ints[i] = Integer.parseInt(objs[i].toString());
        } catch (Exception e) {
          Log.error(null, e);
        }
      }
    }
    return ints;
  }

  public static Float[] toFloatArray(Object[] objs) {
    if (objs == null)
      return null;
    Float[] flas = new Float[objs.length];
    for (int i = 0; i < flas.length; i++) {
      if (objs[i] != null) {
        try {
          flas[i] = Float.parseFloat(objs[i].toString());
        } catch (Exception e) {
          Log.error(null, e);
        }
      }
    }
    return flas;
  }

  public static int toInt(String value) {
    try {
      return Integer.parseInt(value);
    } catch (Exception ex) {
      return 0;
    }
  }

  public static int toInt(String value, int defaultValue) {
    try {
      return Integer.parseInt(value);
    } catch (Exception ex) {
      return defaultValue;
    }
  }

  public static String joinToStr(String[] willJoin, String linkStr) {
    if ((willJoin == null) || (willJoin.length == 0)) {
      return "";
    }

    int i;
    StringBuffer strJoined = new StringBuffer("");

    strJoined.append(willJoin[0]);

    for (i = 1; i < willJoin.length; i++) {
      strJoined.append(linkStr + willJoin[i]);
    }

    return strJoined.toString();
  }

  // Empty checks
  // -----------------------------------------------------------------------
  /**
   * <p>
   * Checks if a String is empty ("") or null.
   * </p>
   * 
   * <pre>
   * StringUtils.isEmpty(null)      = true
   * StringUtils.isEmpty(&quot;&quot;)        = true
   * StringUtils.isEmpty(&quot; &quot;)       = false
   * StringUtils.isEmpty(&quot;bob&quot;)     = false
   * StringUtils.isEmpty(&quot;  bob  &quot;) = false
   * </pre>
   * 
   * <p>
   * NOTE: This method changed in Lang version 2.0. It no longer trims the
   * String. That functionality is available in isBlank().
   * </p>
   * 
   * @param str the String to check, may be null
   * @return <code>true</code> if the String is empty or null
   */
  public static boolean isEmpty(String str) {
    return str == null || str.length() == 0;
  }

  public static void main(String[] args) {
    String s = combinStr("A", "b");
    System.out.println(s);
  }

  public static String[] splitStr(String line, String delim) {
    if (line == null || delim == null)
      return (new String[0]);
    List<String> list = new ArrayList<String>();
    int startIndex = 0, endIndex = 0;
    while ((endIndex = line.indexOf(delim, startIndex)) >= 0) {
      if (startIndex < endIndex)
        list.add(line.substring(startIndex, endIndex));
      startIndex = endIndex + delim.length();
    }
    if (startIndex < line.length())
      list.add(line.substring(startIndex));
    return (String[]) list.toArray(new String[list.size()]);
  }

}
